import axios from 'axios';

const API_URL = 'http://127.0.0.1:5000';

export const analyzeFood = async (imageFile) => {
    try {
        const formData = new FormData();
        formData.append('file', imageFile);

        const response = await axios.post(`${API_URL}/analysis`, formData, {
            headers: {
                'Content-Type': 'multipart/form-data',
            },
        });

        const data = response.data;

        // base64 解码
        // 生成图片 URL
        const imageUrl = `data:image/png;base64,${data.image}`;

        return {
            imageUrl: imageUrl,
            dict: data.dict,
            recommendations: data.recommendations
        };
    } catch (error) {
        console.error('Error analyzing food:', error);
        throw error;
    }
};